/**
 * Created by Wonderchief on 2017/1/12.
 */
// var APPID = "webapp";
// var SERVE = 'http://10.0.0.154:3344';

var html5elmeents = "address|article|aside|audio|canvas|command|datalist|details|dialog|figure|figcaption|footer|header|hgroup|keygen|mark|meter|menu|nav|progress|ruby|section|time|video".split('|');
for(var i = 0; i < html5elmeents.length; i++){
    document.createElement(html5elmeents[i]);
}
var APPID = "webcustomer";
// var APPID = "websales";
var SERVE = '';
//对jq中的ajax请求的封装，处理一些常用库
function req(url, data, fn) {
    //每次加载都修改权限
    setApp();
    function setApp() {
        //权限
        var web2=sessionStorage.getItem('boss');
        if(web2=="websales") {
            APPID = "websales";
        }
    }
    var reqpack = {
        appid: APPID
        , core: {}
        , data: null
        , token: null
    };
    reqpack.data = data;
    //进行装包操作,把本地的信息塞到core里,比如XXXid这些或者token
    if (sessionStorage.customerid != null &&APPID!="websales") {
        reqpack.core.customerid = sessionStorage.customerid;
    }
    if (sessionStorage.websalesman != null&&APPID=="websales") {
        reqpack.core.salesmanid = sessionStorage.websalesman;
    }
    if (sessionStorage.tokenCus != null&&APPID=="webcustomer") {
        reqpack.token = sessionStorage.tokenCus;
    }
    if (sessionStorage.tokenwebsalesman != null&&APPID=="websales") {
        reqpack.token = sessionStorage.tokenwebsalesman;
    }
    if(sessionStorage.openid!=null){
        reqpack.core.openid = sessionStorage.openid;
    }
    if(APPID=="websales"){
        if(sessionStorage.Setcustomer){
            reqpack.data.customerid=sessionStorage.Setcustomer
        }
    }
    //消息处理程序
    var msgHandler=function (pack) {
        switch (pack.msg.type)
        {
            case 0://空消息
                fn(pack);
                break;
            case 1://系统默认消息
                if(pack.msg.data){
                    layer.msg(pack.msg.data)
                }
                fn(pack);
                break;
            case 2://系统异常消息
                if(pack.msg.data){
                    layer.msg(pack.msg.data)
                }
                fn(pack);
                break;
            case 3://控制客户端重定向
                $("#alert").addClass("success");
                location.href=pack.msg.data;
                break;
            case 4://控制客户端弹出消息框
                fn(pack);//这样才可以，为什么
                break;
            case 5://控制sessionStorage
                $.each(pack.msg.data,function (i,obj) {
                    sessionStorage.setItem(i,obj);
                });
                break;
            case 9://控制客户端eval执行js脚本
                eval(pack.msg.data);
                break;

        }
        if(!pack.msg.type&&pack.msg.type!==0){
            fn(pack);//这样才可以，为什么
        }
    };
    //修改了这里
    $.ajax({ url: SERVE+url,type:"post",data:reqpack, success: function(pack){
        //进行解包操作,处理status
        switch(pack.status){
            case 0://正常状态
                //执行msg处理器
                msgHandler(pack);
                break;
            case -1://服务器异常状态
                layer.msg(pack.msg.data);
                break;
            case 1://服务器繁忙状态
                break;
            case 2://没有权限访问
                msgHandler(pack);//marketing的登陆
                break;
            case 3://找不到对应的业务
                break;
        }
    },error:function () {
        error("系统错误!请联系系统管理员");
    }
    });
}
//报错，需要有alert的html
// <!--=====弹出框=====-->
// <div class="alert alert-danger navbar-fixed-top text-center hidden" role="alert" id="alert">
//     <a href="#" class="alert-link" id="error"></a>
//     </div>
// <!--=====end 弹出框=====-->
function error(message){
    var alerts=$("#alert")
    if(alerts.hasClass("hidden")){
        alerts.removeClass("hidden");
        setTimeout((function () {
            $("#alert").addClass("hidden");
            $("#error").html("");
        }),5000)
    }
    $("#error").html(message);
}
//获取url的登陆信息
function getUrlParams(fn) {
    var temp = {};
    var query = location.search;
    query = query.substr(1);
    query = query.split('&');
    query.forEach(function (x) {
        x = x.split('=');
        if (x[0] === 'nickname') {
            temp[x[0]] = decodeURI(x[1]);
        }
        else {
            temp[x[0]] = x[1];
        }
    });

    if (fn != null)fn(temp);
}

//验证微信登陆和业务员登录

function checkBoss() {
    //权限
    var web=sessionStorage.getItem('boss');
    if (!web&&APPID=="webcustomer") {
        // 检查登陆
        return true;
    }
    else {
        return false;
    }
}


//登陆事件
function handleLogin() {
        var href = location.origin + location.pathname;
        sessionStorage.setItem('bossUrl', href);
        getUrlParams(function (temp) {
            console.log(temp);
            if (temp.hasOwnProperty('customerid'))sessionStorage.setItem('customerid', temp.customerid);
            sessionStorage.setItem('redirectQuery', JSON.stringify(temp));
        });
        //调用登陆接口  http://10.0.0.154:3344/customer/weixinLogin/
        location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
            // "appid=wx4a2b7abd550eeadb" +
            // "&redirect_uri=http%3a%2f%2f"+encodeURIComponent("infinitus-beauty.com")+"%2fcustomer%2fweixinlogin%2f" +
            // "appid=wx089f43bd1dae5f5e" +
            // "&redirect_uri=http%3a%2f%2f"+encodeURIComponent("bbmis.pjnice.com")+"%2fcustomer%2fweixinlogin%2f" +
            "appid=wx77f952e833068728"+
            "&redirect_uri=http%3a%2f%2f10.0.0.151%3a3345%2fcustomer%2fweixinlogin%2f" +
            "&response_type=code" +
            "&scope=snsapi_userinfo" +
            "&state=STATE#wechat_redirect";
}



